// /mineSubs/pages//getBaby/getBaby.js
const service = require('./getBabyService.js')
const util = require('../../../utils/util.js')
const utilService = require('../../../utils/utilService.js');

var tapIndex = "0";

Page({
  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["全部订单", "待付款", "待发出", "待收货"],
    activeIndex: 0,
    dataSource1: null,
    items1: [],
    dataSource2: null,
    items2: [],
    dataSource3: null,
    items3: [],
    dataSource4: null,
    items4: [],
    shareMsg: '我发现了一个闲置物品的好平台【趣闲】，特意分享给你！',
    hasNextPage: false,
    startPage: 0,  //开始页
    nextPage: 1,
    loadMoreData: '加载更多……',
    isLoading:true,
  },

  onLoad: function (options) {
    this.getShareMessage(10);

    this.setData({ activeIndex: options.type - 1})    
      this.oneService()
    
      this.twoService()
    
      this.threeService()
    
      this.fourService()
  },
  /**
   * tab change
   */
  tabChange(index) {
    console.log(index.detail);
    tapIndex = index.detail;
    this.setData({
      startPage: 0,
      items1: [],
      items2: [],
      items3: [],
      items4: [],
    })
    switch (tapIndex) {
      case "0":
        this.oneService()
        break;
      case "1":
        this.twoService()
        break;
      case "2":
        this.threeService()
        break;
      case "3":
        this.fourService()
        break;
    }
  },
  /**
   * view触底监听
   */
  lower() {
    var self = this;
    // 当前页是最后一页
    if (!self.data.hasNextPage) {
      self.setData({
        loadMoreData: '暂无更多数据'
      })
      return;
    }
    setTimeout(function () {
      console.log('上拉加载更多');
      self.setData({
        hideBottom: false
      })
      switch (tapIndex) {
        case "0":
          self.oneService()
          break;
        case "1":
          self.twoService()
          break;
        case "2":
          self.threeService()
          break;
        case "3":
          self.fourService()
          break;
      }
    }, 300);
  },
  oneService: function () {
    var that = this
    var param = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage,
      pageSize: 5,
      status: -1
    }
    service.requestProducts(param, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage
      })
      for (let i = 0; i < data.items.length; i++) {
        if (data.items[i].time) {
          let timeStap = util.timeToStamp(data.items[i].time);
          data.items[i].time = util.getDateDiff(timeStap);
        } else {
          data.items[i].time = ""
        }
      }
      that.setData({ dataSource1: data, items1: that.data.items1.concat(array), hasNextPage: data.hasNextPage, isLoading:false });
    });
  },
  twoService: function () {
    var that = this
    var param = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage,
      pageSize: 5,
      status: 0
    }
    service.requestProducts(param, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage
      })
      for (let i = 0; i < data.items.length; i++) {
        if (data.items[i].time) {
          let timeStap = util.timeToStamp(data.items[i].time);
          data.items[i].time = util.getDateDiff(timeStap);
        } else {
          data.items[i].time = ""
        }
      }
      that.setData({ dataSource2: data, items2: that.data.items2.concat(array), hasNextPage: data.hasNextPage, isLoading: false });
    });
  },
  threeService: function () {
    var that = this
    var param = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage,
      pageSize: 5,
      status: 1
    }
    service.requestProducts(param, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage
      })
      for (let i = 0; i < data.items.length; i++) {
        if (data.items[i].time) {
          let timeStap = util.timeToStamp(data.items[i].time);
          data.items[i].time = util.getDateDiff(timeStap);
        } else {
          data.items[i].time = ""
        }
      }
      that.setData({ dataSource3: data, items3: that.data.items3.concat(array), hasNextPage: data.hasNextPage, isLoading: false });
    });
  },
  fourService: function () {
    var that = this
    var param = {
      userId: getApp().globalData.userId,
      startPage: that.data.startPage,
      pageSize: 5,
      status: 2
    }
    service.requestProducts(param, function (data) {
      var array = data.items;
      that.setData({
        startPage: data.nextPage
      })
      for (let i = 0; i < data.items.length; i++) {
        if (data.items[i].time) {
          let timeStap = util.timeToStamp(data.items[i].time);
          data.items[i].time = util.getDateDiff(timeStap);
        } else {
          data.items[i].time = ""
        }
      }
      that.setData({ dataSource4: data, items4: that.data.items4.concat(array), hasNextPage: data.hasNextPage, isLoading: false });
    });
  },
  //商品列表item 点击事件
  selectedItem(data) {
    console.log(data.detail.id)
      wx.navigateTo({
        url: "/pages/orderDetails/OrderDetails?order_id=" + data.detail.id
      })
  },
  //获取分享信息
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        console.log("分享数据", data)
        self.setData({
          shareMsg: data.title
        })
      },
      function (error) {

      }
    );
  },
  //分享
  onShareAppMessage: function (res) {
    var title = this.data.shareMsg
    var path = "pages/home/home?userId=" + getApp().globalData.userId
    return {
      title: title,
      path: path,
    
      success: function (res) {
      },
      fail: function (res) {
      }
    }
  },
})